package cn.zyzpp.controller.resume;

import cn.zyzpp.entity.resume.*;
import cn.zyzpp.service.resume.*;
import cn.zyzpp.service.user.UserService;
import cn.zyzpp.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 我的简历
 * Create by yster@foxmail.com 2018/5/25/025 14:42
 */
@Controller
@RequestMapping("/my")
public class PersonalController {
    @Autowired
    private EducationalService educationalService;
    @Autowired
    private UserService userService;
    @Autowired
    private CampusService campusService;
    @Autowired
    private ExpectationService expectationService;
    @Autowired
    private InternshipService internshipService;
    @Autowired
    private MySelfService mySelfService;
    @Autowired
    private PersonalService personalService;
    @Autowired
    private SkillsHobbyService skillsHobbyService;
    @Autowired
    private WorksShowService worksShowService;
    @Autowired
    private HttpSession session;

    /**
     * 我的简历
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("self")
    @RequiresRoles("user")
    public String getMy(int id, ModelMap map){
        if (id == StringUtil.getIntByString(session.getAttribute("uid"))){
            map.addAttribute("or",1);
        }
        String img = userService.queryUserImg(id).getImgUrl();
        Personal personal = personalService.query(id);
        Expectation expectation = expectationService.query(id);
        List<Educational> educationalList = educationalService.queryAll(id);
        List<Campus> campusList= campusService.queryAll(id);
        List<SkillsHobby> skillsHobbyList = skillsHobbyService.queryAll(id);
        List<WorksShow> worksShowList = worksShowService.queryAll(id);
        MySelf mySelf = mySelfService.query(id);
        map.addAttribute("id",id);
        map.addAttribute("personal",personal);
        map.addAttribute("expectation",expectation);
        map.addAttribute("educationalList",educationalList);
        map.addAttribute("campusList",campusList);
        map.addAttribute("skillsHobbyList",skillsHobbyList);
        map.addAttribute("worksShowList",worksShowList);
        map.addAttribute("mySelf",mySelf);
        return "myself";
    }

    /**
     * 个人信息
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editmy/result")
    public String editmy(Personal personal){
        int uid = StringUtil.getIntBySessionUid(session);
        if(personalService.query(uid)==null){
            personalService.save(personal,uid);
        }else{
            personal.setUid(uid);
            personalService.updataByUid(personal);
        }
        return "编辑成功";
    }
    @RequestMapping("/editmy")
    public String editMyPage(){
        return "/edit/editmy";
    }

    /**
     * 期望实习
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editexper/result")
    public String editExper(Expectation expectation){
        int uid = StringUtil.getIntBySessionUid(session);
        if(expectationService.query(uid)==null){
            expectationService.save(expectation,uid);
        }else {
            expectation.setUid(uid);
            expectationService.updataByUid(expectation);
        }
        return "编辑成功";
    }

    @RequestMapping("/editexper")
    public String editExperPage(){
        return "/edit/editexper";
    }

    /**
     * 教育背景
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editedu/result")
    public String editEdu(Educational educational){
        int uid = StringUtil.getIntBySessionUid(session);
        if(educationalService.query(uid)==null){
            educationalService.save(educational,uid);
        }else {
            educational.setUid(uid);
            educationalService.updataByUid(educational);
        }
        return "编辑成功";
    }

    @RequestMapping("/editedu")
    public String editEduPage(){
        return "/edit/editedu";
    }

    /**
     * 个人经历
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editCampus/result")
    public String editSchool(Campus campus){
        int uid = StringUtil.getIntBySessionUid(session);
        if(campusService.query(uid)==null){
            campusService.save(campus,uid);
        }else {
            campus.setUid(uid);
            campusService.updataByUid(campus);
        }
        return "编辑成功";
    }

    @RequestMapping("/editCampus")
    public String editSchoolPage(){
        return "/edit/editcampus";
    }

    /**
     * 技能爱好
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editHobby/result")
    public String editHobby(SkillsHobby skillsHobby){
        int uid = StringUtil.getIntBySessionUid(session);
        if(skillsHobbyService.query(uid)==null){
            skillsHobbyService.save(skillsHobby,uid);
        }else {
            skillsHobby.setUid(uid);
            skillsHobbyService.updataByUid(skillsHobby);
        }
        return "编辑成功";
    }

    @RequestMapping("/editHobby")
    public String editHobbyPage(){
        return "/edit/edithobby";
    }

    /**
     * 作品展示
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editwork/result")
    public String editWork(WorksShow worksShow){
        int uid = StringUtil.getIntBySessionUid(session);
        if(worksShowService.query(uid)==null){
            worksShowService.save(worksShow,uid);
        }else {
            worksShow.setUid(uid);
            worksShowService.updataByUid(worksShow);
        }
        return "编辑成功";
    }

    @RequestMapping("/editwork")
    public String editWorkPage(){
        return "/edit/editwork";
    }

    /**
     * 个人评价
     */
    @ResponseBody
    @RequiresRoles("user")
    @RequestMapping("/editSelf/result")
    public String editSelf(MySelf mySelf){
        int uid = StringUtil.getIntBySessionUid(session);
        if(mySelfService.query(uid)==null){
            mySelfService.save(mySelf,uid);
        }else {
            mySelf.setUid(uid);
            mySelfService.updataByUid(mySelf);
        }
        return "编辑成功";
    }

    @RequestMapping("/editSelf")
    public String editSelfPage(){
        return "/edit/editself";
    }

}
